package com.it.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.it.commonutils.R;
import com.it.staservice.client.UcenterClient;
import com.it.staservice.entity.StatisticsDaily;
import com.it.staservice.mapper.StatisticsDailyMapper;
import com.it.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author test.java
 * @since 2021-04-26
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    public void registerCount(String day) {
        //添加数据之前 先删除表里相同的数据
        QueryWrapper<StatisticsDaily>wrapper=new QueryWrapper<StatisticsDaily>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用得到一天的注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister=(Integer)registerR.getData().get("countRegister");
        //获取的数据添加到数据库 统计分析
        StatisticsDaily sta=new StatisticsDaily();
        sta.setRegisterNum(countRegister);//注册人数
        sta.setDateCalculated(day);//统计日期
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));

        baseMapper.insert(sta);
    }

    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily>wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList= baseMapper.selectList(wrapper);
        List<String>date_calculatedList=new ArrayList<String>();
        List<Integer>numDataList=new ArrayList<Integer>();
        for (int i = 0; i < staList.size(); i++) {
            StatisticsDaily daily = staList.get(i);
            date_calculatedList.add(daily.getDateCalculated());
            switch (type){
                case"login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case"register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case"video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case"course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object>map=new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
